package managedbean;

import entity.AddressEntity;
import entity.FnBBranchEntity;
import entity.PaymentManagementEntity;
import entity.ServiceEntity;
import entity.ServicePackageEntity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.ejb.EJB;
import session.stateful.StakeholderSessionBean;
import javax.faces.context.FacesContext;
import java.util.List;
import entity.StakeholderEntity;
import java.io.Serializable;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.SelectEvent;
import session.stateful.ServiceSessionBean;

@ManagedBean(name = "tCubeManagerBean")
@SessionScoped
public class TCubeManagerBean implements Serializable {

    @EJB
    private StakeholderSessionBean stakeholderSessionBean;
    @EJB
    private ServiceSessionBean serviceSessionBean;
    private String statusMessage;
    private AddressEntity stakeholderAddress;
    private AddressEntity branchAddress;
    private StakeholderEntity selectedStakeholder;
    private FnBBranchEntity selectedBranch;
    private ServiceEntity selectedService;
    private ServiceEntity service;
    private ServicePackageEntity selectedServicePackage;
    private ServicePackageEntity servicePackage;
    private ServiceEntity[] selectedServices;
    private ServiceEntity[] services;

    public TCubeManagerBean() {
        selectedBranch = new FnBBranchEntity();
        service = new ServiceEntity();
        servicePackage = new ServicePackageEntity();
    }

    public List<ServiceEntity> getListOfServices() {
        return serviceSessionBean.getServices();
    }

    public void initiateServices() {
        selectedServices = serviceSessionBean.getServiceByServicePackageId(selectedServicePackage.getId());
    }

    public List<ServicePackageEntity> getServicePackages() {
        return serviceSessionBean.getServicePackages();
    }

    public void retrieveStakeholderInfo() {
        selectedStakeholder = getStakeholderSessionBean().retrieveStakeholderInfoByUsername(selectedStakeholder.getUsername());
    }

    public List<StakeholderEntity> getStakeholders() {
        return stakeholderSessionBean.getAllStakeholders();
    }

    public String onRowSelectNavigate(SelectEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedStakeholder", event.getObject());

        return "TCubeEditStakeholderInfo?faces-redirect=true";
    }

    public String onRowSelectNavigateService(SelectEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedService", event.getObject());

        return "TCubeEditServiceInfo?faces-redirect=true";
    }

    public String onRowSelectNavigateServicePackage(SelectEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedService", event.getObject());
        
        initiateServices();
        return "TCubeEditServicePackageInfo?faces-redirect=true";
    }

    public void editStakeholderInfo() {
        stakeholderSessionBean.editStakeholderInfo(selectedStakeholder);
    }

    public void editServiceInfo() {
        serviceSessionBean.editServiceInfo(selectedService);
    }

    public void addService() {
        serviceSessionBean.createNewService(service);

        service = new ServiceEntity();
    }

    public void addServicePackage() {
        serviceSessionBean.createNewServicePackage(servicePackage, services);

        servicePackage = new ServicePackageEntity();
        services = null;
    }

    public void editServicePackageInfo(ActionEvent event) {
        for (int i =0; i < selectedServices.length; i++)
            System.out.println(selectedServices[i].getName());
        serviceSessionBean.editServicePackageInfo(selectedServicePackage, selectedServices);
    }

    public StakeholderSessionBean getStakeholderSessionBean() {
        return stakeholderSessionBean;
    }

    public void setStakeholderSessionBean(StakeholderSessionBean stakeholderSessionBean) {
        this.stakeholderSessionBean = stakeholderSessionBean;
    }

    public ServiceSessionBean getServiceSessionBean() {
        return serviceSessionBean;
    }

    public void setServiceSessionBean(ServiceSessionBean serviceSessionBean) {
        this.serviceSessionBean = serviceSessionBean;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public AddressEntity getStakeholderAddress() {
        return stakeholderAddress;
    }

    public void setStakeholderAddress(AddressEntity stakeholderAddress) {
        this.stakeholderAddress = stakeholderAddress;
    }

    public AddressEntity getBranchAddress() {
        return branchAddress;
    }

    public void setBranchAddress(AddressEntity branchAddress) {
        this.branchAddress = branchAddress;
    }

    public StakeholderEntity getSelectedStakeholder() {
        return selectedStakeholder;
    }

    public void setSelectedStakeholder(StakeholderEntity selectedStakeholder) {
        this.selectedStakeholder = selectedStakeholder;
    }

    public FnBBranchEntity getSelectedBranch() {
        return selectedBranch;
    }

    public void setSelectedBranch(FnBBranchEntity selectedBranch) {
        this.selectedBranch = selectedBranch;
    }

    public ServiceEntity getSelectedService() {
        return selectedService;
    }

    public void setSelectedService(ServiceEntity selectedService) {
        this.selectedService = selectedService;
    }

    public ServicePackageEntity getSelectedServicePackage() {
        return selectedServicePackage;
    }

    public void setSelectedServicePackage(ServicePackageEntity selectedServicePackage) {
        this.selectedServicePackage = selectedServicePackage;
    }

    public ServiceEntity[] getSelectedServices() {
        return selectedServices;
    }

    public void setSelectedServices(ServiceEntity[] selectedServices) {
        this.selectedServices = selectedServices;
    }

    public ServicePackageEntity getServicePackage() {
        return servicePackage;
    }

    public void setServicePackage(ServicePackageEntity servicePackage) {
        this.servicePackage = servicePackage;
    }

    public ServiceEntity getService() {
        return service;
    }

    public void setService(ServiceEntity service) {
        this.service = service;
    }

    public ServiceEntity[] getServices() {
        return services;
    }

    public void setServices(ServiceEntity[] services) {
        this.services = services;
    }
    
    // This method is added by Cong Dat
    private List<PaymentManagementEntity> paymentManagementList;
    
    public List<PaymentManagementEntity> getPaymentManagementList() {
        return paymentManagementList;
    }
    
    public void setPaymentManagementList(List<PaymentManagementEntity> paymentManagementList) {
        this.paymentManagementList = paymentManagementList;
    }
    
    public void initBillListForTCube(PhaseEvent event) {
        this.setPaymentManagementList(stakeholderSessionBean.getBillListOfAllStakeholders());
    }
}

